from pymongo import MongoClient


class MongoService():
    def __init__(self):
        self.conn = MongoClient('127.0.0.1', 27017)
        self.db = self.conn.english

    def save_word(self, word):
        collect = self.db.word

        try:
            collect.insert(word)
        except:
            print("insert error:", word)

    def save_example(self, example):
        collect = self.db.example

        try:
            collect.insert(example)
        except:
            print("insert error:", example)

    def word_list(self):
        collect = self.db.word
        word_list = []

        for rec in collect.find().sort("content"):
            word_list.append(rec['content'])

        return word_list

    def find_word(self, word):
        collect = self.db.word

        return collect.find_one({"content": word})